class orderIntro {
  constructor(mongoose) {
    this.mongoose = mongoose
    this.Schema = mongoose.Schema
    // AnimeIntro模板
    this.orderIntro = new this.Schema({

      userId: String,
      id: Number,
      termId: Number,
      coachId: String,
      name: String,
      time: String,
      price: Number,
      number: Number,
      coachReduce: Boolean,
      userReduce: Boolean,
      predict: Boolean,
      placeId: Number,
      sum: Number,
      avatar: String,

    })
    this.modal = this.mongoose.model('Order', this.orderIntro)
  }

  // @paras1 查询条件 动漫名字数组
  // @paras2 查询结果过滤器
  // 返回查询结果
  orderFind(body) {
    if (body.userId) {
      return this.modal.find({ userId: body.userId })
    } else {
      return this.modal.find({ coachId: body.coachId })
    }
  }

  addOrder(body) {
    var arr = ''
    for (let index = 0; index < 16; index++) {
      arr = arr + parseInt(Math.random() * 10)
    }
    let order = new this.modal({
      userId: body.userId,
      id: parseInt(arr),
      name: body.name,
      termId: body.termId,
      coachId: body.coachId,
      time: '',
      price: body.price,
      number: body.number,
      coachReduce: false,
      userReduce: false,
      predict: false,
      sum: body.sum,
      placeId: body.placeId,
      avatar: body.avatar
    })
    return order.save()

  }
  predict(body) {
    return this.modal.updateOne({ id: body.id }, { predict: true, time: body.time })
  }

  complete(body) {
    return this.modal.find({ id: parseInt(body.id) })
  }

  coachUpdate(data) {
    if (data[0].userReduce) {
      return this.modal.updateOne({ id: data[0].id }, { $inc: { number: -1 }, userReduce: false, predict: false })
    } else {
      return this.modal.updateOne({ id: data[0].id }, { coachReduce: true })
    }
  }

  userUpdate(data) {
    if (data[0].coachReduce) {
      return this.modal.updateOne({ id: data[0].id }, { $inc: { number: -1 }, coachReduce: false, predict: false })
    } else {
      return this.modal.updateOne({ id: data[0].id }, { userReduce: true })
    }
  }


}

module.exports = orderIntro
